import { message } from 'antd';
import axios from 'axios';
import Qs from 'qs'; // 引入qs模块，用来序列化post类型的数据

class Api {
  constructor() {
    this.successCode = 2000;
    this.failCode = 500;
    this.baseurl = 'http://localhost:8082';
  }

  getSubForms = function getSubForms(formKey, request, success, fail) {
    this.post(formKey, 'getSubForms', '/api/getSubForms/', request, success, fail);
  };

  testDataSource = function testDataSource(formKey, request, success, fail) {
    this.post(formKey, 'testDataSource', '/api/testDataSource/', request, success, fail);
  };

  getDataSource = function getDataSource(formKey, request, success, fail) {
    this.post(formKey, 'getDataSource', '/api/getDataSource/', request, success, fail);
  };

  saveForm = function (formKey, request, success, fail) {
    // var mock ={form_key:"",form_type:1,form_name:"",form_design_type:0,form_design_model:"",form_control_property_model:""};
    this.post(formKey, 'saveFormDesign', '/api/saveform/', request, success, fail);
  };

  getForm = function (formKey, request, success, fail) {
    // var mock={form_key:""}
    this.post(formKey, 'getFormDesign', '/api/getform/', request, success, fail);
  };

  deleteForm = function (formKey, request, success, fail) {
    this.post(formKey, 'deleteFormDesign', '/api/deleteform/', request, success, fail);
  };

  submitFormTest = function (formKey, request, success, fail) {
    this.postJson(formKey, 'new', '/api/test/', request, success, fail);
  };

  buttonClick = function (formKey, action, request, success, fail) {
    this.postJson(formKey, action, '/api/buttonclick/', request, success, fail);
  };

  postOperator = function (formKey, action, url2, request, callback) {
    const url = url2.indexOf('/') === 0 ? this.baseurl + url2 : url2;
    console.log(url, request);
    axios
      .post(url, Qs.stringify(request), {
        headers: {
          formKey: encodeURIComponent(formKey),
          formAction: encodeURIComponent(action),
        },
      })
      .then((res) => {
        console.log(url, res);
        callback && callback(res);
      });
  };

  post = function (formKey, action, url, request, success, fail) {
    console.log(url, request);
    axios
      .post(this.baseurl + url, Qs.stringify(request), {
        headers: {
          formKey: encodeURIComponent(formKey),
          formAction: encodeURIComponent(action),
        },
      })
      .then((res) => {
        console.log(url, res);
        if (res.data?.code === this.successCode) {
          success(res.data);
        } else {
          fail(res.data);
        }
        if (res.data?.debug instanceof Array && res.data?.debug.length > 0) {
          // 显示debug窗体
          console.log('脚本调试信息', res.data.debug);
          message.info(`脚本调试信息:${JSON.stringify(res.data.debug)}`);
          // alert(res.data.debug);
        }
      });
  };

  postJson = function (formKey, action, url, request, success, fail) {
    console.log(url, request);
    axios
      .post(this.baseurl + url, request, {
        headers: {
          formKey: encodeURIComponent(formKey),
          formAction: encodeURIComponent(action),
          'Content-Type': 'application/json',
        },
      })
      .then((res) => {
        console.log(url, res);
        if (res.data?.code === this.successCode) {
          success(res.data);
        } else {
          fail(res.data);
        }
        if (res.data?.debug instanceof Array && res.data?.debug.length > 0) {
          // 显示debug窗体
          console.log('脚本调试信息', res.data.debug);
          message.info(`脚本调试信息:${JSON.stringify(res.data.debug)}`);
          // alert(res.data.debug);
        }
      });
  };
}
const ApiHelper = new Api();
export default ApiHelper;
